package com.yun2.bms.service.impl;

import com.yun2.bms.dao.IBookDao;
import com.yun2.bms.dao.ICategoryDao;
import com.yun2.bms.dao.impl.BookDaoImpl;
import com.yun2.bms.dao.impl.CategoryDaoImpl;
import com.yun2.bms.model.Book;
import com.yun2.bms.model.Category;
import com.yun2.bms.model.Lend;
import com.yun2.bms.service.IBookService;
import com.yun2.bms.service.ILendService;

import java.util.List;

/**
 * author: youli
 * qq: 27855162@qq.com
 * date: 2020/5/26
 */

public class BookServiceImpl implements IBookService {

    private IBookDao bookDao = new BookDaoImpl();

    private ICategoryDao categoryDao = new CategoryDaoImpl();

    private ILendService lendService = new LendServiceImpl();

    @Override
    public List<Book> getAll() {
        List<Book> list = this.bookDao.getAll();
        for (Book book : list) {
            //设置每本书的分类名称
            Category category = this.categoryDao.get(book.getCategoryId());
            book.setCategoryName(category.getCategoryName());
            //设置每本书的已经发生借阅数量和剩余数量
            Integer lendingQuantity = this.lendService.getLendedNumberByBookId(book.getId());
            book.setLendingQuantity(lendingQuantity);
            book.setRemainNumber(book.getBookNumber() - book.getLendingQuantity());
        }
        return list;
    }

    @Override
    public int delete(String ids) {
        return this.bookDao.delete(ids);
    }

    @Override
    public List<Book> getAllByBookName(String bookName) {
        return this.bookDao.getAllByBookName(bookName);
    }

    @Override
    public int add(Book book) {
        return this.bookDao.add(book);
    }

    @Override
    public int edit(Book book) {
        return this.bookDao.edit(book);
    }

    @Override
    public Book get(int bookId) {
        return this.bookDao.get(bookId);
    }
}
